package com.tensquare.friend.controller;

import com.netflix.discovery.converters.Auto;
import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import jdk.net.SocketFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.PrivateKey;

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendService friendService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserClient userClient;

    @RequestMapping(value="/like/{friendid}/{type}",method= RequestMethod.PUT)
    public Result addFriend(@PathVariable String friendid, @PathVariable String type){
        // 验证是否登录
        Claims claims = (Claims) request.getAttribute("user_claims");
        if (claims == null){
            return new Result(false, StatusCode.ACCESSERROR,"权限不足");
        }
        if (type != null){
            // 如果是喜欢 type = 1
            if (type.equals("1")){
                int flag = friendService.addFriend(claims.getId(), friendid);
                if (flag == 0 ){
                    return new Result(false, StatusCode.ERROR, "不能重复添加好友");
                }else if (flag == 1){
                    userClient.updateFanscountAndFollowcount(claims.getId(), friendid, 1);
                    return new Result(true, StatusCode.OK, "添加成功");
                }else {
                    return new Result(false, StatusCode.ERROR, "添加失败");
                }
            }else if (type.equals("2")){
                int flag = friendService.addNoFriend(claims.getId(), friendid);
                if (flag == 0 ){
                    return new Result(false, StatusCode.ERROR, "不能重复添加非好友");
                }else if (flag == 1){
                    return new Result(true, StatusCode.OK, "添加成功");
                }else {
                    return new Result(false, StatusCode.ERROR, "添加失败");
                }
            }else {
                return new Result(false, StatusCode.ERROR, "参数错误");
            }
        }else {
            return new Result(false, StatusCode.ERROR, "参数错误");
        }
    }

    @RequestMapping(value="/{friendid}",method= RequestMethod.DELETE)
    public Result addFriend(@PathVariable String friendid){
        // 验证是否登录
        Claims claims = (Claims) request.getAttribute("user_claims");
        if (claims == null){
            return new Result(false, StatusCode.ACCESSERROR,"权限不足");
        }
        String userid = claims.getId();
        friendService.deleteFriend(userid, friendid);
        userClient.updateFanscountAndFollowcount(claims.getId(), friendid, -1);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
